﻿// CModifyPassword.cpp: 实现文件
//

#include "stdafx.h"
#include "CModifyPassword.h"
#include "afxdialogex.h"
#include <map>
#include <direct.h>
#include <fstream>
#include "md5.h"
#include "resource.h"

std::map<std::string, std::string> UserInfos;//用户信息
// CModifyPassword 对话框

IMPLEMENT_DYNAMIC(CModifyPassword, CDialogEx)

CModifyPassword::CModifyPassword(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MODIFY_PASSWORD, pParent), _workingDirectory(_getcwd(nullptr, 1000))
{
	
}

CModifyPassword::~CModifyPassword()
{

}

void CModifyPassword::CreateDefaultUserInfo(bool isSetDefualtInfos)
{
	std::string path(_workingDirectory);
	path.append("\\info");
	if (!PathIsDirectory(path.c_str()))
		CreateDirectory(path.c_str(), nullptr);
	path.append("\\UserInfo.txt");
	FILE* fp = nullptr;
	fopen_s(&fp, path.c_str(), "w+");
	//if (fp == nullptr)
	//	fopen_s(&fp, path.c_str(), "r+");
	if (isSetDefualtInfos)
	{
		UserInfos.clear();
		UserInfos.insert(std::map<std::string, std::string>::value_type("Operator", ""));
		UserInfos.insert(std::map<std::string, std::string>::value_type("Engineer", MD5("Engineer").toStr()));
		UserInfos.insert(std::map<std::string, std::string>::value_type("Administrator", MD5("Administrator").toStr()));
	}

	std::string content("Operator,");
	content.append(UserInfos.at("Operator"));
	content.append("\n");
	content.append("Engineer,");
	content.append(UserInfos.at("Engineer"));
	content.append("\n");
	content.append("Administrator,");
	content.append(UserInfos.at("Administrator"));
	fprintf(fp, content.c_str());
	fclose(fp);
}

void CModifyPassword::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_USER_LIST, m_cbUserList);
	DDX_Control(pDX, IDC_OLD_PASSWORD, m_txtOldPassword);
	DDX_Control(pDX, IDC_NEW_PASSWORD, m_txtNewPassword);
	DDX_Control(pDX, IDC_CFM_PASSWORD, m_txtConfirmPassword);
}


BEGIN_MESSAGE_MAP(CModifyPassword, CDialogEx)
	ON_BN_CLICKED(IDC_CONFIRM, &CModifyPassword::OnBnClickedConfirm)
	ON_BN_CLICKED(IDCEXIT, &CModifyPassword::OnBnClickedCexit)
END_MESSAGE_MAP()


// CModifyPassword 消息处理程序


BOOL CModifyPassword::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	HICON m_hIcon;
	m_hIcon = AfxGetApp()->LoadIcon(IDI_CYG_LOGO);
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	m_cbUserList.SetCurSel(0);

	std::string path(_workingDirectory);
	path.append("\\info\\UserInfo.txt");
	std::ifstream fin(path, std::ios::in);
	UserInfos.clear();
	UserInfos.insert(std::map<std::string, std::string>::value_type("Operator", ""));
	UserInfos.insert(std::map<std::string, std::string>::value_type("Engineer", MD5("Engineer").toStr()));
	UserInfos.insert(std::map<std::string, std::string>::value_type("Administrator", MD5("Administrator").toStr()));
	if (fin.good())//如果文件存在,则修改值
	{
		std::string temp;
		while (std::getline(fin, temp))
		{
			size_t index = temp.find(',');
			if (index == -1)
			{
				fin.close();
				MessageBox(_T("Checked that the configuration file is not in the correct format and has been reset to the default configuration!"), "Info", MB_ICONINFORMATION);
				CreateDefaultUserInfo();
				break;
			}
			string name = temp.substr(0, index);
			string password = temp.substr(index + 1);
			if (UserInfos.find(name) != UserInfos.end())
				UserInfos.at(name) = password;
			else
			{
				fin.close();
				MessageBox(_T("Check that the user name in the configuration file has been changed and has been reset to the default configuration!"), "Info", MB_ICONINFORMATION);
				CreateDefaultUserInfo();
				break;
			}
		}
	}
	else 
	{
		CreateDefaultUserInfo();
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CModifyPassword::OnBnClickedConfirm()
{
	// TODO: 在此添加控件通知处理程序代码
	CString userName, userOldPassword, userNewPassword, userConfirmPassword;
	m_cbUserList.GetWindowTextA(userName);
	m_txtOldPassword.GetWindowTextA(userOldPassword);
	m_txtNewPassword.GetWindowTextA(userNewPassword);
	m_txtConfirmPassword.GetWindowTextA(userConfirmPassword);
	std::string _userName(userName.GetBuffer(0));
	if ((userOldPassword.GetLength() == 0 && !UserInfos.at(_userName).empty())
		|| (userOldPassword.GetLength() != 0 && MD5(userOldPassword.GetBuffer(0)).toStr() != UserInfos.at(_userName)))
	{
		MessageBox(_T("Old password error!"), "Error", MB_ICONERROR);
		return;
	}
	if (userNewPassword != userConfirmPassword)
	{
		MessageBox(_T("The new password and confirmation password do not match!"), "Error", MB_ICONERROR);
		return;
	}
	if (userNewPassword.GetLength() == 0)
		UserInfos.at(_userName) = "";
	else
		UserInfos.at(_userName) = MD5(userNewPassword.GetBuffer(0)).toStr();
	CreateDefaultUserInfo(false);
	MessageBox(_T("Password modified successfully!"), "Success", MB_ICONINFORMATION);
	EndDialog(IDOK);
}


void CModifyPassword::OnBnClickedCexit()
{
	// TODO: 在此添加控件通知处理程序代码
	EndDialog(IDCANCEL);
}


BOOL CModifyPassword::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_RETURN:
			OnBnClickedConfirm();
			return true;
		default:
			break;
		}
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}
